﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VIETPHONG
{
    public partial class frmxemhoadon : Form
    {
        public string makhachhang;
        public string tenkhachhang;
        public string masothue;
        public string sotaikhoan;
        public string hinhthucthanhtoan;
        public string diachi;
        public string vungmien;
        VIETPHONGEntities db = new VIETPHONGEntities();
         public frmxemhoadon()
        {
            InitializeComponent();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmxemhoadon_Load(object sender, EventArgs e)
        {            
            dgvxemhoadon.AutoGenerateColumns = false;
            this.CenterToScreen();
            hienthithongtin();
            
            if(String.IsNullOrEmpty(masothue))
            {
                lblhoten.Text = tenkhachhang;
                lbldiachi.Text = diachi;
                lblhttt.Text = hinhthucthanhtoan;
                lblmasothue.Text = "/ / / / / / / /";
                lblsotaikhoan.Text = sotaikhoan;
            }
            else
            {
                lblhoten.Text = tenkhachhang;
                lbldiachi.Text = diachi;
                lblhttt.Text = hinhthucthanhtoan;
                lblmasothue.Text = masothue;
                lblsotaikhoan.Text = sotaikhoan;    
            }
                   
        }

        private void hienthithongtin()
        {
            var query = from khachhang in db.KHACHHANGs 
                        from sanpham in db.SANPHAMs
                        from hoadon in db.HOADONs
                        from chitiethoadon in db.CHITIETHOADONs
                        from xevanchuyen in db.XEVANCHUYENs
                        from loaisanpham in db.LOAISANPHAMs
                        from loaibao in db.LOAIBAOs
                        where khachhang.MAKHACHHANG==hoadon.MAKHACHHANG && hoadon.MAHOADON==chitiethoadon.MAHOADON
                        && sanpham.MASANPHAM==chitiethoadon.MASANPHAM && hoadon.BIENSOXE==xevanchuyen.BIENSOXE
                        && sanpham.MALOAI==loaisanpham.MALOAI && sanpham.MALOAIBAO==loaibao.MALOAIBAO
                        && khachhang.MAKHACHHANG == makhachhang                        
                        select new{
                            khachhang.CHIETKHAU,
                            xevanchuyen.BIENSOXE,
                            hoadon.MAHOADON,
                            hoadon.NGAYLAPHD,
                            sanpham.TENSANPHAM,
                            sanpham.DONVITINH,
                            chitiethoadon.SOLUONG,
                            sanpham.DONGIA,
                            DONGIACHIETKHAU =sanpham.DONGIA -((sanpham.DONGIA * khachhang.CHIETKHAU)/100),
                            THANHTIEN=chitiethoadon.SOLUONG * sanpham.DONGIA,
                            SOBAO=chitiethoadon.SOLUONG/loaibao.GIATRI                            
                        };
                var sum = query.Select(c => c.THANHTIEN).Sum();            
                var thuegtgt = (sum * int.Parse(txtthuegtgt.Text))/100;
                var tongcongtienthanhtoan = sum + thuegtgt;                  
                dgvxemhoadon.DataSource = query.ToList();
                foreach(var kh in query)
                {
                    lblsohoadon.Text = kh.MAHOADON;
                    lblngaylaphoadon.Text = String.Format("{0:d}",kh.NGAYLAPHD);                    
                    lblxevanchuyen.Text = kh.BIENSOXE;
                    //lbltongcong.Text = sum.ToString();
                    lbltongcong.Text = String.Format("{0:0,0}", sum).Replace(",",".")+",00";
                    //lbltienthuegtgt.Text = thuegtgt.ToString();
                    lbltienthuegtgt.Text = String.Format("{0:0,0}", thuegtgt).Replace(",", ".") + ",00";
                    //lbltongcongtienthanhtoan.Text = tongcongtienthanhtoan.ToString();
                    lbltongcongtienthanhtoan.Text = String.Format("{0:0,0}", tongcongtienthanhtoan).Replace(",", ".") + ",00";
                    lbltienvietbangchu.Text = ChucNang.DocSoThanhChu(tongcongtienthanhtoan.ToString()) +" đồng";
                    lbltongsobao.Text = query.Select(b => b.SOBAO).Sum().ToString() +" bao";
                    lbltongsokg.Text = query.Select(kg => kg.SOLUONG).Sum().ToString() + " kg";
                    //lblthanhtien.Text = query.Select(tien => tien.THANHTIEN).Sum().ToString();
                    lblthanhtien.Text = String.Format("{0:0,0}", query.Select(tien => tien.THANHTIEN).Sum()).Replace(",", ".") + ",00";
                }
                for (int i = 0; i < dgvxemhoadon.Rows.Count; i++)
                {
                    dgvxemhoadon.Rows[i].Cells[0].Value = i + 1;
                    int k = int.Parse(dgvxemhoadon.Rows[i].Cells[6].Value.ToString());                                     
                }                            
        }

        private void nhậpKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frmkhachhang frmkh = new Frmkhachhang();
            frmkh.Show();
        }

        private void nhậpSảnPhẩmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSanpham frmsp = new frmSanpham();
            frmsp.Show();
        }
    }
}
